package com.yeswater.multithread.chapter7;

/**
 * author : yeswater
 * date : 2023/11/5
 *
 * 线程关联线程组
 */

public class Run_7_2_1 {
    public static void main(String[] args) throws InterruptedException {
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();
        ThreadGroup group = new ThreadGroup("yeswater-ThreadGroup");

        Thread t3 = new Thread(group, t1);
        Thread t4 = new Thread(group, t2);
        t3.start();
        t4.start();

        System.out.println(" 活动的线程数: " + group.activeCount());
        System.out.println(" 线程组的名称: " + group.getName());
    }

    private static class MyThread extends Thread {

        @Override
        public void run() {
            try {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println("ThreadName=" + Thread.currentThread().getName());
                    Thread.sleep(3000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
